/*
 *  $Id: CurseOfMadness.cs 1063 2010-05-20 17:40:43Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Darkness.Spells {
    public class CurseOfMadness: EnchantmentCard<ChainData> {
        public CurseOfMadness(): base("001_d_s_curse_of_madness") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Darkness;
            Value = 100;
            Target = new ChainTarget(null, GraveyardTarget.Sacrifice, CreatureTarget.Both);
            AddManaCost(ElementType.Darkness, 1);
            AddCondition(new GraveyardCondition(false, false, 1));
        }

        protected override void DoAction(TargetArgs<ChainData> args) {
            TargetArgs<GraveyardData> ga;
            if (!args.Target.GetArgs(0, out ga)) return;
            ga.Target.MoveToSecondDeck();
            base.DoAction(args);
        }
        protected override AbstractObject GetTarget(TargetArgs<ChainData> args) {
            TargetArgs<Creature> ca;
            if (!args.Target.GetArgs(1, out ca)) return null;
            return ca.Target;
        }
        protected override EnchantmentAffect CreateEnchantment() {
            return new CurseOfMadnessAffect();
        }

        private class CurseOfMadnessAffect: EnchantmentAffect<CurseOfMadness> {
            public CurseOfMadnessAffect() {
                Add(new Affects.ModDamage(2));
            }

            public override void SetAffect() {
                base.SetAffect();
                LocalSummary.OnStartDefencePhase += DoDefence;
                LocalSummary.OnUseAbility += DoAbility;
            }
            public override void ResetAffect() {
                base.ResetAffect();
                LocalSummary.OnStartDefencePhase -= DoDefence;
                LocalSummary.OnUseAbility -= DoAbility;
            }

            private void DoDefence() {
                DoDamage();
            }
            private void DoAbility(AbilityCard ability, TargetArgs args) {
                DoDamage();
            }

            private void DoDamage() {
                Creature cr = Owner as Creature;
                if (cr == null) return;
                if (cr.NeighbourLeft != null)
                    cr.NeighbourLeft.DamageBy(cr.NewDamage(1));
                if (cr.NeighbourRight != null)
                    cr.NeighbourRight.DamageBy(cr.NewDamage(1));
            }
        }
    }
}
